package com.zy.openchat.core.model.discover;

import android.content.Context;
import android.content.SharedPreferences;
import android.os.Parcel;
import android.os.Parcelable;

import com.google.gson.Gson;
import com.zy.openchat.App;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class DiscoverMenuSectionInfo implements Parcelable {
    //分组标题
    public String title;
    //菜单列表 - DiscoverMenuInfo
    public List<DiscoverMenuInfo> menus;

    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeString(this.title);
        dest.writeTypedList(this.menus);
    }

    public DiscoverMenuSectionInfo() {
    }

    protected DiscoverMenuSectionInfo(Parcel in) {
        this.title = in.readString();
        this.menus = in.createTypedArrayList(DiscoverMenuInfo.CREATOR);
    }

    public static final Creator<DiscoverMenuSectionInfo> CREATOR = new Creator<DiscoverMenuSectionInfo>() {
        @Override
        public DiscoverMenuSectionInfo createFromParcel(Parcel source) {
            return new DiscoverMenuSectionInfo(source);
        }

        @Override
        public DiscoverMenuSectionInfo[] newArray(int size) {
            return new DiscoverMenuSectionInfo[size];
        }
    };

    //本地缓存
    public static List<DiscoverMenuSectionInfo> getDiscoverMenus() {
        Context context = App.getInstanse();
        String jsonStr = context.getSharedPreferences("discover", Context.MODE_PRIVATE).getString("menus", null);
        if (jsonStr != null)
            return new ArrayList<>(Arrays.asList(new Gson().fromJson(jsonStr, DiscoverMenuSectionInfo[].class)));
        return null;
    }

    public static void saveDiscoverMenus(List<DiscoverMenuSectionInfo> list) {
        if (list == null) {
            return;
        }
        String jsonStr = new Gson().toJson(list);
        Context context = App.getInstanse();
        SharedPreferences.Editor editor = context.getSharedPreferences("discover", Context.MODE_PRIVATE).edit();
        editor.putString("menus", jsonStr);
        editor.apply();
    }
}
